 /*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */
package SharedDrawing;
import common.XmlConstants;

/**
 * the chat class is the one where chat messages append
 * Controller of the Chat functionality
 */

public class Chat {
    
    private static Chat instance;
    private SharedDrawingUI.UIWorker worker;

    private Chat() {
        this.worker = SharedDrawingUI.getInstance().getWorker();
    }
    
    public static Chat getInstance()
    {
        if(instance == null)
        {
            instance = new Chat();
        }
        return instance;
    }

    /**
     * appends the given text in the jTextArea representing the chatBox in GUI
     * @param emitterId
     * @param text 
     */
    public void append(String emitterId, String text) {
        System.out.println("append");
        Action action = new Action(XmlConstants.chat);
        action.addContent(XmlConstants.emitterId, emitterId);
        action.addContent(XmlConstants.message, text);
        worker.push(action);
    }
    
    /**
     * appends the given text in the chatbox and sends it to the server.
     */
    public void send()
    {
        if(!"".equals(SharedDrawingUI.getjTextSendChatBox().getText()))
        {
            SharedDrawingUI.getjTextAreaChatBox().append("<"+ConnexionManager.getInstance().getNickname()+">"+SharedDrawingUI.getjTextSendChatBox().getText()+'\n');
            Action attr = new Action(XmlConstants.chat);
            attr.addContent(XmlConstants.message, SharedDrawingUI.getjTextSendChatBox().getText());
            SharedDrawingUI.getjTextSendChatBox().setText("");
            XmlGenerator.sendXml(attr);
        }
    }
}
